package com.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;

import com.bean.Project;
import com.bean.Room;
import com.service.ProjectService;
import com.service.RoomService;

@Controller
@RequestMapping("room")
public class RoomAction {
	@Autowired
	private RoomService service ;
	@Autowired
	private ProjectService pservice;
	
	//列表页面
	@RequestMapping("toList.do")
	public String toList(String name,String complexnumber,String uniteNumber,String roomnumber,Model model){
		Map<String, Object> map = new HashMap<String,Object>();
		if(name != null && "".equals(name.trim())){
			name = null;
		}
		if(complexnumber != null && "".equals(complexnumber.trim())){
			complexnumber = null;
		}
		if(uniteNumber != null && "".equals(uniteNumber.trim())){
			uniteNumber = null;
		}
		if(roomnumber != null && "".equals(roomnumber.trim())){
			roomnumber = null;
		}
		map.put("name", name);
		map.put("complexnumber", complexnumber);
		map.put("uniteNumber", uniteNumber);
		map.put("roomnumber", roomnumber);
		List<Room> list = service.select(map);
		model.addAttribute("list", list);
		model.addAttribute("name",name);
		return"room/list";
	}
	
	
	//跳转添加页面
	@RequestMapping("toAdd.do")
	public String toAdd(Model model){
		Map<String, Object> map = new HashMap<String,Object>();
		List<Project> list = pservice.select(map);
		model.addAttribute("list", list);
		return "room/add";
	}
	
	
	//添加
	@RequestMapping("add.do")
	public String add(Room room) throws ParseException{
		Float area = room.getArea();
		Float roomprice = room.getRoomprice();
		room.setRoomtotal(area*roomprice);
		room.setState("未开盘");
		service.insert(room);
		return "redirect:toList.do";
	}
	
	
	
	//删除
	@RequestMapping("delete.do")
	public String delete(Integer id){
		service.deleteByPrimaryKey(id);
		return "redirect:toList.do";
	}
	
	
	
//	//跳转修改
//	@RequestMapping("toUpdate.do")
//	public String toUpdate(Integer id,Model model){
//		Project project = service.selectByPrimaryKey(id);
//		model.addAttribute("project", project);
//		return "project/update";
//	}
//	
//	//修改
//	@RequestMapping("update.do")
//	public String update(Project project){
//		service.updateByPrimaryKeySelective(project);
//		return "redirect:toList.do";
//	}

}
